#coding:utf-8
from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from homeapp.BLL.ttest_service import ttest_service
from homeapp.BLL.tnote_service import tnote_service
from homeapp.models import TNote
import time
import datetime
import json
import uuid

class note:

    @staticmethod
    def index(request):

        # 查询数据库
        items = tnote_service.selectall()
        obj = {
            "viewmodel":{
                "items":items
            }
        }
        # print obj["viewmodel"]["items"][0]
        return render(request, "homeapp/note_index.html", obj)

 	# return render(request, "homeapp/note_index.html", pageModel)

    @staticmethod
    def ajax_addnote(request):

        ajaxmodel = {}

        # 得到输入的标题
        TxtValue = request.POST.get("title")

        # 得到当前的时间
        thistime = time.localtime()
        strtime = time.strftime("%Y-%m-%d", thistime)
        LogDateTime = datetime.datetime.strptime(strtime, "%Y-%m-%d")

        # guid
        guidvalue=str(uuid.uuid1())

        # 作者的id（临时）
        AGValue = str(uuid.uuid1())

        # NoteType_Guid
        NoteType_Guid = "cfd58bcb-f96d-11e5-ac22-000c29a84f46"

        # Path
        ajaxmodel["guid"] = guidvalue
        ajaxmodel["Account_Guid"] = AGValue
        # ajaxmodel["NoteDateTime"] = time.strftime("%Y-%m-%d",LogDateTime)
        ajaxmodel["Path"] = ""
        ajaxmodel["Text"] = TxtValue
        ajaxmodel["NoteType_Guid"] = NoteType_Guid

        # 添加到库
        newnote = TNote(guid=guidvalue
                        ,Account_Guid=AGValue
                        ,NoteDateTime=LogDateTime
                        ,Path=""
                        ,Text=TxtValue
                        ,NoteType_Guid = "cfd58bcb-f96d-11e5-ac22-000c29a84f46")
        newnote.save()

        return HttpResponse(json.dumps(ajaxmodel), content_type="application/json")
"""
        retdata = {}
        retdata['name'] = 'abc'
        retdata['pwd'] = 'cde'
        retdata['arr'] = [{
            "name":123
            },
            {
            "name":123,
            "pwd":333
            }]
        return HttpResponse(json.dumps(retdata), content_type="application/json")
        #
"""
"""
import json
from django.http import HttpResponse

response_data = {}
response_data['result'] = 'failed'
response_data['message'] = 'You messed up'
return HttpResponse(json.dumps(response_data), content_type="application/json")
"""
